<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\model\ProductsGroup;


class ProductGroup extends BaseController
{
    public function index($limit=10)
    {
        $name = request()->param('name');
        $status = request()->param('status');
        $time = request()->param('time');
        $data = ProductsGroup::with(['cate','pp','dw'])
            ->order('id', 'desc')
            ->when($name, function ($query) use ($name) {
                $query->where('name', 'like', "%$name%");
            })->when($status, function ($query) use ($status) {
                $query->where('status', $status);
            })->when($time, function ($query) use ($time) {
                $query->whereBetween('create_time', $time);
            })->paginate($limit);
        return $this->success($data);
    }

    public function create()
    {
        $params = request()->param();
        $params['number'] = getNumber('產品組合');
        ProductsGroup::create($params);
        return $this->success();
    }

    public function update($id)
    {
        ProductsGroup::where('id',$id)->update(request()->param());
        return $this->success();
    }

    public function delete($id)
    {
        ProductsGroup::where('id','in',$id)->delete();
        return $this->success();
    }
}
